package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.StudentScore;
import com.example.result.Result;
import com.example.service.StudentScoreService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学生分数控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class StudentScoreController {
    @Autowired
    StudentScoreService studentScoreService;

    // ===============================学生分数管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加学生分数信息
     */
    @RequestMapping(value = "/studentScores", method = RequestMethod.POST)
    public Result addStudentScore(@RequestBody String json) {
        // 将JSON数据转换为StudentScore对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentScore studentScore = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentScore.class);

        return studentScoreService.addStudentScore(studentScore);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新学生分数信息
     */
    @RequestMapping(value = "/studentScores", method = RequestMethod.PUT)
    public Result updateStudentScore(@RequestBody String json) {
        // 将JSON数据转换为StudentScore对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentScore studentScore = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentScore.class);
        return studentScoreService.updateStudentScore(studentScore);

    }

    /**
     * @param studentScoreId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据学生分数编号删除学生分数信息
     */
    @RequestMapping(value = "/studentScores/{studentScoreId}", method = RequestMethod.DELETE)
    public Result deleteStudentScoreById(@PathVariable("studentScoreId") String studentScoreId) {
        return studentScoreService.deleteStudentScoreById(studentScoreId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有学生分数信息
     */
    @RequestMapping(value = "/studentScores", method = RequestMethod.GET)
    public Result queryAllStudentScore() {
        return studentScoreService.queryAllStudentScore();

    }

    /**
     * @param studentScoreId
     * @return 以JSON格式返回该学生分数信息
     * @describe 根据编号查询学生分数信息
     */
    @RequestMapping(value = "/studentScores/{studentScoreId}", method = RequestMethod.GET)
    public Result queryStudentScoreById(@PathVariable("studentScoreId") String studentScoreId) {
        return studentScoreService.queryStudentScoreById(studentScoreId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学生分数信息
     */
    @RequestMapping(value = "/studentScores/excels", method = RequestMethod.POST)
    public Result addStudentScoreByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<StudentScore> list = JSON.parseArray(parseObject.getString("json"), StudentScore.class);

        System.out.println(list);

        return studentScoreService.addStudentScoreByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生分数编号批量删除学生分数信息
     */
    @RequestMapping(value = "/studentScores/dels", method = RequestMethod.POST)
    public Result deleteStudentScoreByIds(@RequestBody Map<String, String> Ids) {

        return studentScoreService.deleteStudentScoreByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学生分数信息模糊查询
     */
    @RequestMapping(value = "/studentScores/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return studentScoreService.serachByInfo(info);

    }
}
